package com.dtz.rabbitmq.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Producer {

	public static void main(String[] args) {
		// 1、创建连接工厂
		ConnectionFactory factory = new ConnectionFactory();
		factory.setVirtualHost("/");
		factory.setHost("1.15.137.13");
		factory.setPort(5672);
		factory.setUsername("admin");
		factory.setPassword("admin");
		
		Connection connection = null;
		Channel channel = null;
		
		try {
			
			connection = factory.newConnection();
			
			channel = connection.createChannel();
			
			// 声名交换机
			String exchangeName = "topic-test";
			channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC, true);
			
			// 声名队列
			String queuen1 = "t-queuen1";
			channel.queueDeclare(queuen1, true, false, false, null);
			String queuen2 = "t-queuen2";
			channel.queueDeclare(queuen2, true, false, false, null);
			
			// 绑定队列
			channel.queueBind(queuen1, exchangeName, "#.order.#");
			channel.queueBind(queuen2, exchangeName, "*.td.*");
			
			// 发送消息
			channel.basicPublish(exchangeName, "com.order.tt.t", null, "com.order.tt.t".getBytes());
			channel.basicPublish(exchangeName, "com.td.order", null, "com.td.order".getBytes());
			
			
			
		} catch (Exception e) {
			// TODO: handle exception
		} finally
		{
			// 9: 释放连接关闭通道
            if (channel != null && channel.isOpen()) {
                try {
                    channel.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
		}
	}
	
}
